<script setup lang="ts">
import { PropType, reactive, ref, watch } from 'vue'
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { useValidator } from '@/hooks/web/useValidator'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  actionType: {
    type: String as PropType<'add' | 'edit'>,
    default: 'add'
  }
})

const formSchema = reactive<FormSchema[]>([
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false, // 默认不显示
    colProps: {
      span: 12
    },
    componentProps: {
      disabled: true, // 设为禁用状态
      placeholder: '系统自动生成'
    }
  },
  {
    field: 'serial_number',
    label: '序列号',
    component: 'Input',
    colProps: {
      span: 12
    },
    componentProps: {
      placeholder: '请输入摄像头序列号',
      clearable: true
    }
  },
  {
    field: 'model',
    label: '型号',
    component: 'Input',
    colProps: {
      span: 12
    },
    componentProps: {
      placeholder: '请输入摄像头型号',
      clearable: true
    }
  },
  {
    field: 'ip_address',
    label: 'IP地址',
    component: 'Input',
    colProps: {
      span: 12
    },
    componentProps: {
      placeholder: '请输入摄像头IP地址',
      clearable: true
    }
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    colProps: {
      span: 12
    },
    componentProps: {
      placeholder: '请选择摄像头状态',
      clearable: true,
      options: [
        { label: '在线', value: 'online' },
        { label: '离线', value: 'offline' },
        { label: '维护中', value: 'maintenance' }
      ]
    },
    value: 'online'
  },
  {
    field: 'room_id',
    label: '房间ID',
    component: 'InputNumber',
    colProps: {
      span: 12
    },
    componentProps: {
      placeholder: '请输入房间ID',
      min: 1,
      controlsPosition: 'right',
      style: {
        width: '100%'
      }
    }
  },
  {
    field: 'last_active',
    label: '最后活动时间',
    component: 'DatePicker',
    colProps: {
      span: 12
    },
    componentProps: {
      type: 'datetime',
      placeholder: '请选择最后活动时间',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      style: {
        width: '100%'
      }
    }
  }
])

const rules = reactive({
  serial_number: [required()],
  ip_address: [required()],
  status: [required()],
  room_id: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const validate = async () => {
  const elForm = await getElFormExpose()
  return await elForm?.validate().catch(() => false)
}

watch(
  [() => props.currentRow, () => props.actionType],
  ([currentRow, actionType]) => {
    if (!currentRow) return

    // 设置ID字段显示状态
    formSchema.find((item) => item.field === 'id')!.show = actionType === 'edit'

    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  validate,
  getFormData
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" label-width="100px" />
</template>
